package com.landary.parameterrplacement.controller;

import com.landary.parameterrplacement.entity.*;
import com.landary.parameterrplacement.service.AnalysisUserXmlService;
import com.landary.parameterrplacement.service.DeptParamsConversionService;
import com.landary.parameterrplacement.service.PostParamsConversionService;
import com.landary.parameterrplacement.service.UserParamsConversionService;
import com.landary.parameterrplacement.utils.WebServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘欣
 * @createTime 2023/8/1 16:41
 * @description
 */
@RestController
@RequestMapping("/requesrParamConversion")
@Slf4j
public class RequestParamConversionController {

    @Autowired
    private DeptParamsConversionService deptParamsConversionService;

    @Autowired
    private UserParamsConversionService userParamsConversionService;

    @Autowired
    private PostParamsConversionService postParamsConversionService;


    @Autowired
    private WebServiceClient webServiceClient;

    @Autowired
    private AnalysisUserXmlService analysisUserXmlService;

    /**
     * 门户组织参数转换
     *
     * @param dept
     * @return
     * @throws Exception
     */
    @RequestMapping("/portalDeptParamsToMkDeptParams")
    @ResponseBody
    public MkOrgDept portalDeptParamsToMkDeptParams(@RequestBody Dept dept) throws Exception {
        log.info("接受到的参数：" + dept);
        //调用人员同步webservice接口
        String responseXml = webServiceClient.getUsers("appPORTALLoginEnable", "ou=orgs,ou=131601,o=avic");
        //人员虚拟
//                        String responseXml = "<respMsg><header><sysName>APP_UUMS</sysName><isSuccess>true</isSuccess><message>操作成功</message></header><document><![CDATA[<users>" +
//                "<user><dn>cn=18200415, ou=21400,ou=214,ou=214,o=avic </dn><cn>18200415</cn><avicCorpCode>131601</avicCorpCode><avicSimpleTitle>部长</avicSimpleTitle><mail>shixiangchao@sy601.com</mail><avicUserAccount>shixiangchao</avicUserAccount><avicDeptName>能力与总体与IT部办公室</avicDeptName><avicSortOrder>200</avicSortOrder><avicDeptCode>21400</avicDeptCode><avicEmployeeType>正式职工</avicEmployeeType><avicIdCardNumber>210103198211282117</avicIdCardNumber><avicUserSecurity>2</avicUserSecurity><avicLocalCode>60103441</avicLocalCode><fullName>石祥超</fullName><avicCorpName>航空工业沈阳飞机设计研究所</avicCorpName></user>"+
//                                "<user><dn>cn=18200415, ou=21400,ou=214,ou=214,o=avic </dn><cn>18200415</cn><avicCorpCode>131601</avicCorpCode><avicSimpleTitle>一般工作人员</avicSimpleTitle><mail>lilan@sy601.com</mail><avicUserAccount>lilan</avicUserAccount><avicDeptName>能力与总体与IT部办公室</avicDeptName><avicSortOrder>200</avicSortOrder><avicDeptCode>21400</avicDeptCode><avicEmployeeType>正式职工</avicEmployeeType><avicIdCardNumber>210103198211282117</avicIdCardNumber><avicUserSecurity>2</avicUserSecurity><avicLocalCode>60103441</avicLocalCode><fullName>李兰</fullName><avicCorpName>航空工业沈阳飞机设计研究所</avicCorpName></user>"+
//                                "<user><dn>cn=18200415, ou=21407,ou=214,ou=214,o=avic </dn><cn>18200415</cn><avicCorpCode>131601</avicCorpCode><avicSimpleTitle>副室主任</avicSimpleTitle><mail>sunyan@sy601.com</mail><avicUserAccount>sunyan</avicUserAccount><avicDeptName>能力与总体与IT应用室</avicDeptName><avicSortOrder>200</avicSortOrder><avicDeptCode>21407</avicDeptCode><avicEmployeeType>正式职工</avicEmployeeType><avicIdCardNumber>210103198211282117</avicIdCardNumber><avicUserSecurity>2</avicUserSecurity><avicLocalCode>60103441</avicLocalCode><fullName>孙岩</fullName><avicCorpName>航空工业沈阳飞机设计研究所</avicCorpName></user>"+
//                                "<user><dn>cn=18200415, ou=21407,ou=214,ou=214,o=avic </dn><cn>18200415</cn><avicCorpCode>131601</avicCorpCode><avicSimpleTitle>团委书记</avicSimpleTitle><mail>sunyan@sy601.com</mail><avicUserAccount>ceshiyong</avicUserAccount><avicDeptName>能力与总体与IT应用室</avicDeptName><avicSortOrder>200</avicSortOrder><avicDeptCode>21407</avicDeptCode><avicEmployeeType>正式职工</avicEmployeeType><avicIdCardNumber>210103198211282117</avicIdCardNumber><avicUserSecurity>2</avicUserSecurity><avicLocalCode>60103441</avicLocalCode><fullName>测试用户</fullName><avicCorpName>航空工业沈阳飞机设计研究所</avicCorpName></user>"+
//                                "</users>]]></document></respMsg>";
        //部门虚拟数据
//        String responseXml = "<respMsg><header><sysName>APP_UUMS</sysName><isSuccess>true</isSuccess><message>操作成功</message></header><document><![CDATA[<users>" +
//                "<user><dn>cn=18200415, ou=21400,ou=214,ou=214,o=avic </dn><cn>18200415</cn><avicCorpCode>131601</avicCorpCode><avicSimpleTitle>部长</avicSimpleTitle><mail>wuping@sy601.com</mail><avicUserAccount>wuping</avicUserAccount><avicDeptName>能力与总体与IT部办公室</avicDeptName><avicSortOrder>200</avicSortOrder><avicDeptCode>21400</avicDeptCode><avicEmployeeType>正式职工</avicEmployeeType><avicIdCardNumber>210103198211282117</avicIdCardNumber><avicUserSecurity>2</avicUserSecurity><avicLocalCode>60103441</avicLocalCode><fullName>吴平</fullName><avicCorpName>航空工业沈阳飞机设计研究所</avicCorpName></user>"+
//                "</users>]]></document></respMsg>";

//        log.info("组织人员webservice返回参数" + responseXml);
//        String responseXmlAvic = "<respMsg>" +
//                "<header>" +
//                "<sysName>APP_UUMS</sysName>" +
//                "<isSuccess>true</isSuccess>" +
//                "<message>操作成功</message>" +
//                "</header>" +
//                "<document>" +
//                "<![CDATA[<users>" +
//                "<user><dn>cn=18200415, ou=21400,ou=214,ou=214,o=avic </dn><cn>18200415</cn><avicCorpCode>131601</avicCorpCode><avicSimpleTitle>一般工作人员</avicSimpleTitle><mail>lilan@sy601.com</mail><avicUserAccount>lilan</avicUserAccount><avicDeptName>能力与总体与IT部办公室</avicDeptName><avicSortOrder>200</avicSortOrder><avicDeptCode>21400</avicDeptCode><avicEmployeeType>正式职工</avicEmployeeType><avicIdCardNumber>210103198211282117</avicIdCardNumber><avicUserSecurity>2</avicUserSecurity><avicLocalCode>60103441</avicLocalCode><fullName>李兰</fullName><avicCorpName>航空工业沈阳飞机设计研究所</avicCorpName></user>\"+\n" +
//                "<user><dn>cn=18200415, ou=21407,ou=214,ou=214,o=avic </dn><cn>18200415</cn><avicCorpCode>131601</avicCorpCode><avicSimpleTitle>副室主任</avicSimpleTitle><mail>sunyan@sy601.com</mail><avicUserAccount>sunyan</avicUserAccount><avicDeptName>能力与总体与IT应用室</avicDeptName><avicSortOrder>200</avicSortOrder><avicDeptCode>21407</avicDeptCode><avicEmployeeType>正式职工</avicEmployeeType><avicIdCardNumber>210103198211282117</avicIdCardNumber><avicUserSecurity>2</avicUserSecurity><avicLocalCode>60103441</avicLocalCode><fullName>孙岩</fullName><avicCorpName>航空工业沈阳飞机设计研究所</avicCorpName></user>\"+\n" +
//                "<user><dn>cn=18200415, ou=21407,ou=214,ou=214,o=avic </dn><cn>18200415</cn><avicCorpCode>131601</avicCorpCode><avicSimpleTitle>团委书记</avicSimpleTitle><mail>sunyan@sy601.com</mail><avicUserAccount>ceshiyong</avicUserAccount><avicDeptName>能力与总体与IT应用室</avicDeptName><avicSortOrder>200</avicSortOrder><avicDeptCode>21407</avicDeptCode><avicEmployeeType>正式职工</avicEmployeeType><avicIdCardNumber>210103198211282117</avicIdCardNumber><avicUserSecurity>2</avicUserSecurity><avicLocalCode>60103441</avicLocalCode><fullName>测试用户</fullName><avicCorpName>航空工业沈阳飞机设计研究所</avicCorpName></user>\"+\n" +
//                "<user><dn>cn=18709299, ou=21407,ou=214,ou=214,o=avic </dn><cn>18709299</cn><avicCorpCode>131601</avicCorpCode><avicSimpleTitle>部长</avicSimpleTitle><mail>liuxiaodong1@sy601.com</mail><avicUserAccount>liuxiaodong1</avicUserAccount><avicDeptName>能力与总体与IT部办公室</avicDeptName><avicSortOrder>200</avicSortOrder><avicDeptCode>21400</avicDeptCode><avicEmployeeType>正式职工</avicEmployeeType><avicIdCardNumber>210103198211282117</avicIdCardNumber><avicUserSecurity>2</avicUserSecurity><avicLocalCode>60103441</avicLocalCode><fullName>刘晓冬</fullName><avicCorpName>航空工业沈阳飞机设计研究所</avicCorpName></user>\"+\n" +
//                "</users>]]>" +
//                "</document>" +
//                "</respMsg>";
        List<User> users = analysisUserXmlService.userXmlToMap(responseXml, "user");
        MkOrgDept mkOrgDeptMessage = deptParamsConversionService.deptParamsConversion(dept, users);
        return mkOrgDeptMessage;
    }

    /**
     * 门户用户参转换
     *
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping("/protalUserParamsToMkDeptParams")
    @ResponseBody
    public MkOrgUser protalUserParamsToMkDeptParams(@RequestBody User user) throws Exception {
//        log.info("接受到的参数：" + user);
        MkOrgUser mkOrgUser = userParamsConversionService.userParamsConversion(user);
        return mkOrgUser;
    }

    /**
     * 门户岗位参转换
     *
     * @param post
     * @return
     * @throws Exception
     */
    @RequestMapping("/protalPostParamsToMkPostParams")
    @ResponseBody
    public MKOrgPost protalPostParamsToMkPostParams(@RequestBody Post post) throws Exception {
//        log.info("接受到的参数：" + post);
        MKOrgPost mkOrgPost = postParamsConversionService.psotParamsConversion(post);
        return mkOrgPost;
    }


    @RequestMapping("/createOrg")
    public List<MkOrgDept> createOrg() {
        log.info("开始创建创建组织机构");
        //OrgDept orgDept = new OrgDept();
        List<MkOrgDept> mkOrgDepts = new ArrayList<>();
        MkOrgDept mkOrgDept = new MkOrgDept();
        mkOrgDept.setFdId("org:" + 0);
        mkOrgDept.setFdIsAvailable(true);
        mkOrgDept.setFdName("中国航空工业集团公司沈阳飞机设计研究所");
//        mkOrgDept.setFdName("测试机构");
        mkOrgDept.setFdNo("null");
        mkOrgDept.setFdSourceSystem("601portal");
        mkOrgDept.setFdOrgType("org");
        mkOrgDept.setFdOrder(1);
        mkOrgDepts.add(mkOrgDept);
//        orgDept.setFinish(true);
//        orgDept.setDepts(mkOrgDepts);
        return mkOrgDepts;
    }


}
